<?php

namespace app\admin\controller;

use think\Controller;
use think\Cookie;
use think\Db;
use think\Exception;

class Intercept extends Controller
{
    public function _initialize()
    {
        $token = cookie::get('token');
        if (!empty($token)) {
            $time = time();
            $sql = Db::table('cms_blog_admin')->where('token', $token)->select();
            foreach ($sql as $v) {
                if ($v['token'] == $token && $v['login_time'] > $time) {
                    return ['username' => $v['username'], 'token' => $v['token']];
                } else {
                    $this->redirect('/admin/login');
                }
            }
        } else {
            $this->redirect('/admin/login');
        }
    }
}